<?php
require_once dirname(__FILE__) . '/libraries/ServiceBase.php';
/**
 * Dividend Service （分红服务）
 */
class SDOServiceDividend extends ServiceBase
{
    // 正式环境开通分红服务
    const DIVDEND_OP_URL = 'DIVIDEND_URL';
    // 测试环境开通分红服务
    const PRE_DIVDEND_OP_URL = 'PRE_DIVIDEND_URL';
    // 操作默认值
    const OPERATION_DEFAULT = 1;
    // 附加信息默认值
    const APPENDIX_DEFAULT = 'openIII';
    const OPERATION_OPEN = 1;
    const OPERATION_CLOSE = 0;
    // 错误代码，信息
    protected static $errCodeMsg = array(
        - 1 => "SPID或APPID为空" , 
        - 2 => "SPID+APPID对应的游戏信息不存在" , 
        - 3 => "通知邮件发送失败" , 
        - 1001 => "SP编码为空" , 
        - 1002 => "游戏编码为空" , 
        - 1003 => "公司名称为空" , 
        - 1004 => "公司地址为空" , 
        - 1005 => "公司邮箱为空" , 
        - 1006 => "电话号码为空" , 
        - 1008 => "公司接口人为空" , 
        - 1010 => "分红操作为空" , 
        - 1011 => "分红操作类型不正确" , 
        - 1012 => "分红操作类型不在合法范围内"
    );
    
    /**
     * 开通SDO分红服务
     * 
     * @param string $message 错误信息
     * @param array $params =array( spId => 公司ID,
     * appId => 产品ID,
     * spName => 公司名称,
     * spAddr => 公司地址,
     * spIntroduce => 产品说明(选填),
     * spPhone => 公司电话,
     * spMobile => 公司手机(可为空),
     * spUser => 负责人姓名,
     * spEMail => 邮箱)
     * @param string $env 
     * @param int $operation 0 关闭，1开通
     * @return boolean 
     */
    public static function SetIGWConfig (&$message, $params, $env = 'pre', $operation=self::OPERATION_DEFAULT)
    {
        $need = array(
            'spId' , 
            'appId' , 
            'spName' , 
            'spAddr' , 
            'spPhone' , 
            'spUser' , 
            'spEMail'
        );
        if (! self::_validateParam($params, $need, $message)) {
            self::setErrNo(SDOServiceErrorCode::API_EC_PARAM);
            return false;
        }
        $op = 'SetIGWConfig';
        $url = ($env === SDK_ENV_TEST) ? sdk_fetch_config(SDK_ENV . self::PRE_DIVDEND_OP_URL) : sdk_fetch_config(SDK_ENV . self::DIVDEND_OP_URL);
        if (! isset($params['spMobile'])) {
            $params['spMobile'] = '';
        }
        if (! isset($params['spIntroduce'])) {
            $params['spIntroduce'] = '';
        }
        $params = iconv_array($params, "UTF-8", "GB2312");
        $params['operation'] = $operation;
        $params['appendix'] = self::APPENDIX_DEFAULT;
        $tmpUrl = strtr(http_build_query($params), array(
            "&amp;" => "&"
        ));
        
        $content = self::getRestfulContent($url . '?' . $tmpUrl, $message);
        $errMsg = array(SDK_LOG_METHOD=> __FUNCTION__ , SDK_LOG_PARAMS=> $tmpUrl, SDK_LOG_ENV=>$env);
        if ($content===false) {
            $errMsg[SDK_LOG_EXCEPTION] = $message;
            $errMsg[SDK_LOG_LOCATION] = $url;
            self::fatal($errMsg, __CLASS__);
        	return false;
        }
        $errMsg["return"] = $content;
        if ($content !== "0") {
            $logLevel = "error";
            if (isset(self::$errCodeMsg[$content])) {
            	self::setErrNo($content);
            	$logLevel = "warn";
            }
            $errMsg[SDK_LOG_LOCATION] = $url;
            self::$logLevel($errMsg, __CLASS__);
            $message =  self::$errCodeMsg[$content];     
            return false;
        }
        self::log($errMsg, __CLASS__, true);
        return true;
    }
	/**
     * 自检函数
     */
    public static function selfTest() 
    {
    	$arr = array(
        	"http://igw.178.sdo.com/BPlan/Index.aspx",
        	sdk_fetch_config(SDK_ENV .self::PRE_DIVDEND_OP_URL)
    	);
    	return self::curlTest($arr);
    }
} 
